import PySimpleGUI as sg

import Index
from entity.User import User

user = User
from dbUtils import Db_Connection as db


# 登录操作
def loginService(window_lg):
    while True:
        event, values = window_lg.read(timeout=100)  # 窗口的读取，有两个返回值(1.事件  2.值)
        if event is None:  # 窗口关闭事件
            break
        if event == '-login-':
            user.username = values['-uname-']
            user.password = values['-key-']
            sql = "SELECT * FROM users WHERE username='" + user.username + "' and password='" + user.password + "'"
            data = db.getData(sql)
            if len(data) == 0:
                sg.popup("登录失败！")
            else:
                sg.popup("登录成功！")
                window_lg.close()
                Index.main()
        if event == '-cancel-':
            window_lg.close()
        # 注册按钮
        # if event == '-reg-':
        #     window_lg.close()
        #     market.reg()
